﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AppMain
{
    public partial class TxUsrCtrl : UserControl
    {
        private int txNum = 0;
        Button btnTxControl;
        public event EventHandler chBoxTxIfChanged;
        public delegate void ChangeRequestInPLC(int txNum, E_BtnType button, object Tag);
        public event ChangeRequestInPLC OnChangeRequestInPLC;
        public enum E_BtnType
        {
            Command = 0,
            Power,
            Modulation,
            Channel
        }

        public TxUsrCtrl(int txNumber)
        {
            InitializeComponent();
            txNum = txNumber;
            chBoxTxIF.Tag = txNum;
            grpTxControls.Text = string.Format("TX {0}", txNum);
            createButtons();
        }

        public void updateDisplay(RequestToTx currentReq, SettingsTx setting)
        {
            //Update labels
            if (currentReq.GenModulation) { lblModGen.Text = "GENERATOR"; lblModGen.BackColor = Color.Orange; } else { lblModGen.Text = "NORMAL"; lblModGen.BackColor = Color.LimeGreen; }
            if (currentReq.GenPower) { lblPwrGen.Text = "GENERATOR"; lblPwrGen.BackColor = Color.Orange; } else { lblPwrGen.Text = "NORMAL"; lblPwrGen.BackColor = Color.LimeGreen; }
            if (currentReq.HeatOnForMinTime) { lblHeating.Text = "READY"; lblHeating.BackColor = Color.LimeGreen; } else { lblHeating.Text = "NOT READY"; lblHeating.BackColor = Color.Yellow; }
                
            foreach (Control btn in this.grpTxRqst.Controls)
            {
                //Update all buttons
                if (btn.GetType() == btnTxControl.GetType())
                {           //Update the text and color of the buttons
                    
                    E_BtnType btnType;
                    Enum.TryParse<E_BtnType>(btn.Name, out btnType);
                    switch (btnType)
                    {
                        case E_BtnType.Command:
                            {
                                if ((currentReq.Command == (Thompson.Tx.E_CommandTx)btn.Tag) && (setting.Command == (Thompson.Tx.E_CommandTx)btn.Tag))
                                { btn.BackColor = Color.LimeGreen; }
                                else if (currentReq.Command == (Thompson.Tx.E_CommandTx)btn.Tag)
                                { btn.BackColor = Color.Red; }
                                else if (setting.Command == (Thompson.Tx.E_CommandTx)btn.Tag)
                                { btn.BackColor = Color.Yellow; }
                                else
                                { btn.BackColor = Color.Transparent; }
                                break;
                            }
                        case E_BtnType.Power:
                            {
                                if ((currentReq.PowerRequest == (int)btn.Tag) && (setting.PowerLevel.ToString() + "%" == btn.Text))
                                { btn.BackColor = Color.LimeGreen; }
                                else if (currentReq.PowerRequest == (int)btn.Tag)
                                { btn.BackColor = Color.Red; }
                                else if (setting.PowerLevel.ToString() + "%" == btn.Text)
                                { btn.BackColor = Color.Yellow; }
                                else
                                { btn.BackColor = Color.Transparent; }
                                if (currentReq.GenPower == false)
                                { btn.Text = setting.PowerLevelNormal[(int)btn.Tag].ToString() + "%"; }
                                else
                                { btn.Text = setting.PowerLevelGenerator[(int)btn.Tag].ToString() + "%"; }
                                break;
                            }
                        case E_BtnType.Modulation:
                            {
                                if ((currentReq.ModulationRequest == (int)btn.Tag) && (setting.Modulation == (int)btn.Tag))
                                { btn.BackColor = Color.LimeGreen; }
                                else if (currentReq.ModulationRequest == (int)btn.Tag)
                                { btn.BackColor = Color.Red; }
                                else if (setting.Modulation == (int)btn.Tag)
                                { btn.BackColor = Color.Yellow; }
                                else
                                { btn.BackColor = Color.Transparent; }
                                if (currentReq.GenModulation == false)
                                { btn.Text = setting.ModulationNormal[(int)btn.Tag].ToString(); }
                                else
                                { btn.Text = setting.ModulationGenerator[(int)btn.Tag].ToString(); }
                                break;
                            }
                        case E_BtnType.Channel:
                            {
                                if ((currentReq.Channel == (int)btn.Tag) && (setting.Channel == (int)btn.Tag))
                                { btn.BackColor = Color.LimeGreen; }
                                else if (currentReq.Channel == (int)btn.Tag)
                                { btn.BackColor = Color.Red; }
                                else if (setting.Channel == (int)btn.Tag)
                                { btn.BackColor = Color.Yellow; }
                                else
                                { btn.BackColor = Color.Transparent; }
                                break;
                            }
                        default:
                            break;
                    }
                }
            }
        } //end public void updateDisplay(RequestToTx currentReq, SettingsTx setting)

        public void WriteSnmpStatus(string status)
        {
            lblSnmpStatus.Text = status;
        }

        void createButtons()
        {
            //Create Command/Power/Modulation buttons
            for (int row = 0; row < 3; row++)
            {
                int columns;
                E_BtnType bName = (E_BtnType)row;
                int bWidth;
                int bHeight = 30;
                int bSpacing;
                if (row == 0)
                {   //Command
                    columns = 3;
                    bWidth = 90;
                    bSpacing = 7;
                }
                else if (row == 1)
                {   //Power
                    columns = 4;
                    bWidth = 65;
                    bSpacing = 8;
                }
                else
                {   //Modulation
                    columns = 4;
                    bWidth = 65;
                    bSpacing = 8;
                }
                for (int column = 0; column < columns; column++)
                {
                    btnTxControl = new Button();
                    //Tag is used as Request Value
                    //Text for Power and Mod will be replaced with settings value read from file
                    if (bName != E_BtnType.Command)
                    {
                        btnTxControl.Tag = column;
                        btnTxControl.Text = bName.ToString() + column.ToString();
                    }
                    else
                    {
                        if (column == 0)
                        {
                            btnTxControl.Text = "OFF";
                            btnTxControl.Tag = Thompson.Tx.E_CommandTx.GeneralShutdown;
                        }
                        else if (column == 1)
                        {
                            btnTxControl.Text = "Fillaments";
                            btnTxControl.Tag = Thompson.Tx.E_CommandTx.HeatingOn;
                        }
                        else
                        {
                            btnTxControl.Text = "ON";
                            btnTxControl.Tag = Thompson.Tx.E_CommandTx.GeneralOn;
                        }
                    }
                    btnTxControl.Name = bName.ToString();  //Name used for Request Type
                    btnTxControl.Font = new System.Drawing.Font("Microsoft Sans Serif", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    btnTxControl.Size = new System.Drawing.Size(bWidth, bHeight);
                    btnTxControl.Location = new System.Drawing.Point(6 + column * (bWidth + bSpacing), 20 + row * (bSpacing + bHeight));
                    btnTxControl.UseVisualStyleBackColor = true;
                    btnTxControl.Click += new EventHandler(btnTxControl_Click);
                    this.grpTxRqst.Controls.Add(btnTxControl);
                }
            }

            //Create channel buttons
            int channel = 1;
            for (int row = 0; row < 3; row++)
            {
                for (int column = 0; column < 5; column++)
                {
                    int bWidth = 35;
                    int bHeight = 25;
                    int bSpacing = 6;
                    btnTxControl = new Button();
                    btnTxControl.Text = channel.ToString();
                    btnTxControl.Tag = channel;
                    btnTxControl.Name = ((E_BtnType)((int)3)).ToString();  //Name used for Request Type
                    btnTxControl.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    btnTxControl.Size = new System.Drawing.Size(bWidth, bHeight);
                    btnTxControl.Location = new System.Drawing.Point(6 + column * (bWidth + bSpacing), 150 + row * (bSpacing + bHeight));
                    btnTxControl.UseVisualStyleBackColor = true;
                    btnTxControl.Click += new EventHandler(btnTxControl_Click);
                    grpTxRqst.Controls.Add(btnTxControl);
                    channel++;
                }
            }
        } //end private void createButtons()

        void btnTxControl_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            DialogResult userResponse = MessageBox.Show( string.Format("New command for TX {0} \r\nAre you sure you want to change the {1} to {2}?\n\rThe Transmitter may respond immediately!", txNum.ToString(), btn.Name, btn.Text), 
                                                        "Set new Control Value?", 
                                                        MessageBoxButtons.YesNo, 
                                                        MessageBoxIcon.Warning);
            if (userResponse == DialogResult.Yes)
            {
                E_BtnType buttonType;
                Enum.TryParse<E_BtnType>(btn.Name, out buttonType);
                OnChangeRequestInPLC(this.txNum, buttonType, btn.Tag);
            }
        } //end void btnTxControl_Click(object sender, EventArgs e)

        private void chBoxTxIF_CheckedChanged(object sender, EventArgs e)
        {
            if (chBoxTxIF.Checked)
            {
                chBoxTxIF.BackColor = Color.LimeGreen;
                chBoxTxIF.Text = "Auto";
            }
            else
            {
                chBoxTxIF.BackColor = Color.Orange;
                chBoxTxIF.Text = "Manual";
            }
            chBoxTxIfChanged(sender, e);
        } 
    }
}
